package com.lzy.huijinjiazu.mvp.ui.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.flyco.roundview.RoundTextView;
import com.gyf.barlibrary.ImmersionBar;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.lzy.huijinjiazu.R;
import com.lzy.huijinjiazu.di.component.DaggerSearchComponent;
import com.lzy.huijinjiazu.di.module.SearchModule;
import com.lzy.huijinjiazu.mvp.contract.SearchContract;
import com.lzy.huijinjiazu.mvp.presenter.SearchPresenter;
import com.lzy.huijinjiazu.mvp.ui.widget.TagGroup;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class SearchActivity extends BaseActivity<SearchPresenter> implements SearchContract.View {


    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.et_search)
    EditText mEtSearch;
    @BindView(R.id.iv_clear)
    ImageView mIvClear;
    @BindView(R.id.tv_search)
    TextView mTvSearch;
    @BindView(R.id.tg_history)
    TagGroup mTgHistory;
    @BindView(R.id.history)
    LinearLayout mHistory;
    @BindView(R.id.tg_hot)
    TagGroup mTgHot;
    @BindView(R.id.hot)
    LinearLayout mHot;
    @BindView(R.id.ll_title)
    LinearLayout mLlTitle;

    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerSearchComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .searchModule(new SearchModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(Bundle savedInstanceState) {
        return R.layout.activity_search; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mEtSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                mIvClear.setVisibility(editable.length() > 0 ? View.VISIBLE : View.GONE);
            }
        });

        initTag();
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.statusBarColor(R.color.black).init();
        mImmersionBar.setTitleBarMarginTop(this, mLlTitle);
    }

    private void initTag() {
        mTgHistory.setTagViewFactory(new TagGroup.TagViewFactory() {
            @Override
            public View create(ViewGroup group, int position, Object tag) {
                View view = LayoutInflater.from(SearchActivity.this).inflate(R.layout.item_tag, group, false);
                RoundTextView tvView = (RoundTextView) view;
                tvView.getDelegate().setBackgroundColor(0xffF0F2F3);
                tvView.setText(tag.toString());
                return view;
            }
        });
        mTgHot.setTagViewFactory(new TagGroup.TagViewFactory() {
            @Override
            public View create(ViewGroup group, int position, Object tag) {
                View view = LayoutInflater.from(SearchActivity.this).inflate(R.layout.item_tag, group, false);
                RoundTextView tvView = (RoundTextView) view;
                tvView.getDelegate().setBackgroundColor(0xffF0F2F3);
                tvView.setText(tag.toString());
                return view;
            }
        });

        List<String> data = new ArrayList<>();
        data.add("化妆品");
        data.add("男装");
        data.add("海澜之家");
        data.add("手镯");
        data.add("电视机");
        data.add("扫地机器人");
        mTgHistory.setList(data);

        List<String> data1 = new ArrayList<>();
        data1.add("化妆品");
        data1.add("男装");
        data1.add("海澜之家");
        data1.add("手镯");
        data1.add("电视机");
        data1.add("扫地机器人");
        mTgHot.setList(data1);

    }

    @OnClick({R.id.iv_back, R.id.iv_clear, R.id.tv_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_clear:
                mEtSearch.setText("");
                mIvClear.setVisibility(View.GONE);
                break;
            case R.id.tv_search:
                ArmsUtils.startActivity(SearchSuccessActivity.class);
                break;
        }
    }

}
